// exports.AssetsManager = void 0;
// var AL_GAME_HUA_AssetsUtils = require("./AL_GAME_HUA_AssetsUtils");
// var AL_GAME_HUA_Decorate = require("./AL_GAME_HUA_Decorate");
// var l = "[AssetsManager]";
// var u = (function () {
//     function e() {}
//     e.prototype.loadRes = function (e) {
//         return __awaiter(this, void 0, Promise, function () {
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             return __generator(this, function () {
//                 t = AL_GAME_HUA_AssetsUtils.AssetsUtils.huoqu_teding_ziyuan_lujing_xinxi(e);
//                 n = t.url;
//                 o = t.root;
//                 i = t.bundle;
//                 r = t.type;
//                 this.getBundle({
//                     nameOrUrl: i,
//                     success: function (t) {
//                         if (t.get(n, r)) {
//                             var a = t.get(n, r);
//                             a["@bunlde"] = i;
//                             if (e.progress) {
//                                 e.progress(1, 1, null);
//                             }
//                             return void (e.success && e.success(a));
//                         }
//                         t.load(n, r, e.progress, function (t, n) {
//                             if (t) {
//                                 Log.e(l, o + "加载资源失败: " + t);
//                                 return void (e.fail && e.fail(t));
//                             }
//                             n["@bunlde"] = i;
//                             if (e.success) {
//                                 e.success(n);
//                             }
//                         });
//                     },
//                     fail: e.fail
//                 });
//                 return [2];
//             });
//         });
//     };
//     e.prototype.jiazai_ziyuan_shuzu = function (e) {
//         return __awaiter(this, void 0, Promise, function () {
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var c;
//             return __generator(this, function () {
//                 t = e.urls;
//                 n = e.bundle;
//                 o = e.type;
//                 i = [];
//                 for (r = 0; r < t.length; r++) {
//                     c = AL_GAME_HUA_AssetsUtils.AssetsUtils.huoqu_teding_ziyuan_lujing_xinxi({
//                         url: t[r],
//                         bundle: n,
//                         type: o
//                     });
//                     i.push(c.url);
//                 }
//                 this.getBundle({
//                     nameOrUrl: n,
//                     success: function (t) {
//                         t.load(i, o, e.progress, function (t, o) {
//                             if (t) {
//                                 Log.e(l, "加载资源列表失败: " + t);
//                                 return void (e.fail && e.fail(t));
//                             }
//                             for (var i = 0; i < o.length; i++) {
//                                 o[i]["@bunlde"] = n;
//                             }
//                             if (e.success) {
//                                 e.success(o);
//                             }
//                         });
//                     },
//                     fail: e.fail
//                 });
//                 return [2];
//             });
//         });
//     };
//     e.prototype.loadDir = function (e) {
//         return __awaiter(this, void 0, Promise, function () {
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             return __generator(this, function () {
//                 t = AL_GAME_HUA_AssetsUtils.AssetsUtils.huoqu_teding_ziyuan_lujing_xinxi(e);
//                 n = t.url;
//                 o = t.root;
//                 i = t.bundle;
//                 r = t.type;
//                 this.getBundle({
//                     nameOrUrl: i,
//                     success: function (t) {
//                         t.loadDir(n, r, e.progress, function (t, n) {
//                             if (t) {
//                                 Log.e(l, "加载目录" + o + "失败: " + t);
//                                 return void (e.fail && e.fail(t));
//                             }
//                             for (var r = 0; r < n.length; r++) {
//                                 n[r]["@bunlde"] = i;
//                             }
//                             if (e.success) {
//                                 e.success(n);
//                             }
//                         });
//                     },
//                     fail: e.fail
//                 });
//                 return [2];
//             });
//         });
//     };
//     e.prototype.loadRemote = function (e) {
//         return __awaiter(this, void 0, Promise, function () {
//             return __generator(this, function () {
//                 cc.assetManager.loadRemote(e.url, e.params, function (t, n) {
//                     if (t) {
//                         Log.e(l, "加载远程资源失败,url:" + e.url + ", err: " + t);
//                         return void (e.fail && e.fail(t));
//                     }
//                     if (e.success) {
//                         e.success(n);
//                     }
//                 });
//                 return [2];
//             });
//         });
//     };
//     e.prototype.getBundle = function (e) {
//         return __awaiter(this, void 0, Promise, function () {
//             return __generator(this, function () {
//                 return cc.assetManager.bundles.has(e.nameOrUrl)
//                     ? (e.success && e.success(cc.assetManager.bundles.get(e.nameOrUrl)), [2])
//                     : (this.loadBundle(e), [2]);
//             });
//         });
//     };
//     e.prototype.loadBundle = function (e) {
//         return __awaiter(this, void 0, Promise, function () {
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             return __generator(this, function () {
//                 t = e.options;
//                 n = e.nameOrUrl;
//                 o = e.success;
//                 i = e.fail;
//                 r = e.complete;
//                 Log.ts(l + "加载bundle:" + n);
//                 cc.assetManager.loadBundle(n, t, function (e, t) {
//                     if (e) {
//                         Log.e(l, "加载bundle" + n + "失败: " + e);
//                         Log.te(l + "加载bundle:" + n);
//                         if (i) {
//                             i(e);
//                         }
//                         return void (r && r(e));
//                     }
//                     Log.te(l + "加载bundle:" + n);
//                     if (o) {
//                         o(t);
//                     }
//                 });
//                 return [2];
//             });
//         });
//     };
//     __decorate([AL_GAME_HUA_Decorate.Decorate.promisify], e.prototype, "loadRes", null);
//     __decorate([AL_GAME_HUA_Decorate.Decorate.promisify], e.prototype, "jiazai_ziyuan_shuzu", null);
//     __decorate([AL_GAME_HUA_Decorate.Decorate.promisify], e.prototype, "loadDir", null);
//     __decorate([AL_GAME_HUA_Decorate.Decorate.promisify], e.prototype, "loadRemote", null);
//     __decorate([AL_GAME_HUA_Decorate.Decorate.promisify], e.prototype, "getBundle", null);
//     __decorate([AL_GAME_HUA_Decorate.Decorate.promisify], e.prototype, "loadBundle", null);
//     return __decorate([AL_GAME_HUA_Decorate.Decorate.instance], e);
// })();
// exports.AssetsManager = u;


exports.AssetsManager = void 0;
var AssetsUtils = require("./AL_GAME_HUA_AssetsUtils");
var Decorate = require("./AL_GAME_HUA_Decorate");
var logPrefix = "[AssetsManager]";

/**
 * 资源管理类，用于加载资源和管理资源包
 */
var AssetsManager = (function () {
    function AssetsManager() {}

    /**
     * 异步加载资源
     * @param {Object} options 加载参数，包含资源路径信息、进度回调和成功/失败回调
     * @returns {Promise<void>}
     */
    AssetsManager.prototype.loadRes = function (options) {
        return __awaiter(this, void 0, Promise, function () {
            var pathInfo, url, root, bundle, type;
            return __generator(this, function () {
                pathInfo = AssetsUtils.AssetsUtils.huoqu_teding_ziyuan_lujing_xinxi(options);
                url = pathInfo.url;
                root = pathInfo.root;
                bundle = pathInfo.bundle;
                type = pathInfo.type;

                this.getBundle({
                    nameOrUrl: bundle,
                    success: function (loadedBundle) {
                        var asset = loadedBundle.get(url, type);
                        if (asset) {
                            asset["@bundle"] = bundle;
                            options.progress && options.progress(1, 1, null);
                            options.success && options.success(asset);
                        } else {
                            loadedBundle.load(url, type, options.progress, function (error, resource) {
                                if (error) {
                                    Log.e(logPrefix, root + "加载资源失败: " + error);
                                    options.fail && options.fail(error);
                                    return;
                                }
                                resource["@bundle"] = bundle;
                                options.success && options.success(resource);
                            });
                        }
                    },
                    fail: options.fail
                });

                return [2];
            });
        });
    };

    /**
     * 异步加载资源数组
     * @param {Object} options 加载参数，包含多个资源URL、资源包和资源类型及其回调
     * @returns {Promise<void>}
     */
    AssetsManager.prototype.jiazai_ziyuan_shuzu = function (options) {
        return __awaiter(this, void 0, Promise, function () {
            var urls, bundle, type, urlList, i, pathInfo;
            return __generator(this, function () {
                urls = options.urls;
                bundle = options.bundle;
                type = options.type;
                urlList = [];

                for (i = 0; i < urls.length; i++) {
                    pathInfo = AssetsUtils.AssetsUtils.huoqu_teding_ziyuan_lujing_xinxi({
                        url: urls[i],
                        bundle: bundle,
                        type: type
                    });
                    urlList.push(pathInfo.url);
                }

                this.getBundle({
                    nameOrUrl: bundle,
                    success: function (loadedBundle) {
                        loadedBundle.load(urlList, type, options.progress, function (error, resources) {
                            if (error) {
                                Log.e(logPrefix, "加载资源列表失败: " + error);
                                options.fail && options.fail(error);
                                return;
                            }
                            resources.forEach(resource => {
                                resource["@bundle"] = bundle;
                            });
                            options.success && options.success(resources);
                        });
                    },
                    fail: options.fail
                });

                return [2];
            });
        });
    };

    /**
     * 异步加载目录资源
     * @param {Object} options 加载参数，包含目录路径信息及其回调
     * @returns {Promise<void>}
     */
    AssetsManager.prototype.loadDir = function (options) {
        return __awaiter(this, void 0, Promise, function () {
            var pathInfo, url, root, bundle, type;
            return __generator(this, function () {
                pathInfo = AssetsUtils.AssetsUtils.huoqu_teding_ziyuan_lujing_xinxi(options);
                url = pathInfo.url;
                root = pathInfo.root;
                bundle = pathInfo.bundle;
                type = pathInfo.type;

                this.getBundle({
                    nameOrUrl: bundle,
                    success: function (loadedBundle) {
                        loadedBundle.loadDir(url, type, options.progress, function (error, resources) {
                            if (error) {
                                Log.e(logPrefix, "加载目录" + root + "失败: " + error);
                                options.fail && options.fail(error);
                                return;
                            }
                            resources.forEach(resource => {
                                resource["@bundle"] = bundle;
                            });
                            options.success && options.success(resources);
                        });
                    },
                    fail: options.fail
                });

                return [2];
            });
        });
    };

    /**
     * 异步加载远程资源
     * @param {Object} options 包含远程资源URL和请求参数及其回调
     * @returns {Promise<void>}
     */
    AssetsManager.prototype.loadRemote = function (options) {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function () {
                cc.assetManager.loadRemote(options.url, options.params, function (error, resource) {
                    if (error) {
                        Log.e(logPrefix, "加载远程资源失败,url:" + options.url + ", err: " + error);
                        options.fail && options.fail(error);
                        return;
                    }
                    options.success && options.success(resource);
                });
                return [2];
            });
        });
    };

    /**
     * 获取资源包
     * @param {Object} options 包含资源包名称URL及其回调
     * @returns {Promise<void>}
     */
    AssetsManager.prototype.getBundle = function (options) {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function () {
                // 检查资源包是否已存在
                return cc.assetManager.bundles.has(options.nameOrUrl)
                    ? (options.success && options.success(cc.assetManager.bundles.get(options.nameOrUrl)), [2])
                    : (this.loadBundle(options), [2]);
            });
        });
    };

    /**
     * 异步加载资源包
     * @param {Object} options 包含资源包名称URL、加载选项及其回调
     * @returns {Promise<void>}
     */
    AssetsManager.prototype.loadBundle = function (options) {
        return __awaiter(this, void 0, Promise, function () {
            var bundleOptions, nameOrUrl, successCallback, failCallback, completeCallback;
            return __generator(this, function () {
                bundleOptions = options.options;
                nameOrUrl = options.nameOrUrl;
                successCallback = options.success;
                failCallback = options.fail;
                completeCallback = options.complete;

                Log.ts(logPrefix + "加载bundle:" + nameOrUrl);
                cc.assetManager.loadBundle(nameOrUrl, bundleOptions, function (error, bundle) {
                    if (error) {
                        Log.e(logPrefix, "加载bundle" + nameOrUrl + "失败: " + error);
                        Log.te(logPrefix + "加载bundle:" + nameOrUrl);
                        if (failCallback) {
                            failCallback(error);
                        }
                        completeCallback && completeCallback(error);
                        return;
                    }
                    Log.te(logPrefix + "加载bundle:" + nameOrUrl);
                    successCallback && successCallback(bundle);
                });

                return [2];
            });
        });
    };

    // 应用装饰器
    __decorate([Decorate.Decorate.promisify], AssetsManager.prototype, "loadRes", null);
    __decorate([Decorate.Decorate.promisify], AssetsManager.prototype, "jiazai_ziyuan_shuzu", null);
    __decorate([Decorate.Decorate.promisify], AssetsManager.prototype, "loadDir", null);
    __decorate([Decorate.Decorate.promisify], AssetsManager.prototype, "loadRemote", null);
    __decorate([Decorate.Decorate.promisify], AssetsManager.prototype, "getBundle", null);
    __decorate([Decorate.Decorate.promisify], AssetsManager.prototype, "loadBundle", null);
    return __decorate([Decorate.Decorate.instance], AssetsManager);
})();
exports.AssetsManager = AssetsManager;